<template>
  <iframe :src="url" class="border-none h-full w-full"></iframe>
</template>

<script>
  import paths from "@/api/paths";

  const fileViewUrl = window.config.VUE_APP_FILE_VIEW
  const baseUrl = window.config.VUE_APP_BASE_URL
  const intranet = window.config.VUE_APP_BASE_URL_INTRANET || window.config.VUE_APP_BASE_URL
  const fix = window.config.VUE_APP_BASE_URL.endsWith('/') ? '' : '/'
  export default {
    name: 'FileView',
    data () {
      return {
        url: ''
      }
    },
    props: {
      params: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    components: {},
    created () {
    },
    mounted () {
      if (this.params.url) {
        this.url = this.params.url
      } else {
        this.getFileUrl()
      }
    },
    computed: {},
    methods: {
      getFileUrl () {
        const extension = this.params.extension.toLowerCase()
        const ext = extension.substring(extension.lastIndexOf('.'))
        const showType = this.params.showType
        let viewMode = 'pdfjs'
        let mimeType = ''
        if (['.ofd'].includes(ext)) {
          viewMode = 'ofdjs'
        } else if (['.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx'].includes(ext)) {
          viewMode = 'pdfjs'
        } else if (['.jpg', '.jpeg', '.png', '.bmp', '.gif'].includes(ext)) {
          viewMode = 'img'
        } else if (['.pdf'].includes(ext)) {
          mimeType = '&mimetype=application/pdf'
        } else if (['.txt'].includes(ext)) {
          mimeType = '&mimetype=text/plain;charset=utf-8'
        }
        if (showType === 'wopi') {
          this.url = baseUrl + paths.project.wopiShowDistDoc.replace(/\{materialId\}/, this.params.id).replace(/\{readOnly\}/, true)
          return
        }
        if (mimeType) {
          this.url = baseUrl + fix + 'materialInst/file/download?materialFileId=' + this.params.id + mimeType
        } else {
          if (['.ofd'].includes(ext)) { // ofd转pdf预览，否则会有问题
            this.url = fileViewUrl + 'cvt/preview?sourceFormat=ofd&targetFormat=pdf' +
              '&sourceFile=' + encodeURIComponent(intranet + 'materialInst/file/download?materialFileId=' + this.params.id)
          } else {
            this.url = fileViewUrl + 'preview?viewMode=' + viewMode +
              '&fileUrl=' + intranet + 'materialInst/file/download?materialFileId=' + this.params.id
          }
        }
      }
    }
  }
</script>
<style scoped lang="less">
  .h-full{
    height: calc(100% - 6px);
  }
</style>
